package com.jp.knowledge.http;

import android.content.Context;

import com.google.gson.JsonObject;
import com.jp.knowledge.comm.InfoDetailData;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.InfoData;
import com.jp.knowledge.model.InfoDetailStateModel;
import com.jp.knowledge.model.TopicInfo;
import com.jp.knowledge.model.TopicListInfo;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.List;


/**
 * 用于获取头条详情各种数据的工具类
 * Created by 528787067@qq.com on 2017/1/13.
 */

public class ToutiaoGetDataUtil implements ResultSubscriber.OnResultListener {

    public static final int GET_DETAIL_DATA = 0x01;             // 获取头条详情
    public static final int GET_TOPIC_DATA = 0x02;              // 获取话题数据
    public static final int GET_TOPIC_LIST_REFRESH = 0x03;     // 下拉刷新获取话题列表
    public static final int GET_TOPIC_LIST_LOAD = 0x04;         // 上啦加载获取话题列表
    public static final int GET_DETAIL_STATE = 0x05;            // 资讯详情状态

    private Context context;
    private InfoDetailData infoDetailData;


    public ToutiaoGetDataUtil(Context context){
        this.context = context;
        this.infoDetailData = InfoDetailData.getInfoDetail();
    }

    /**
     * 获取头条详情数据
     * @return 代表请求提交是否成功
     */
    public boolean getDetailData(){
        if(infoDetailData.getLinkId() == null || infoDetailData.getLinkId().trim().length() == 0)
            return false;
        JsonObject param = new JsonObject();
        param.addProperty("token", HttpHelper.token);
        param.addProperty("id", infoDetailData.getLinkId());
        param.addProperty("type", infoDetailData.getLinkType());
        HttpHelper.getInstance(context).getInfoDataDetail(param, GET_DETAIL_DATA, this);
        return true;
    }

    /**
     * 获取话题数据
     * @return 代表请求提交是否成功
     */
    public boolean getTopicData(){
//        if(infoDetailData.getLinkId() == null || infoDetailData.getLinkId().trim().length() == 0)
//            return false;
//        JsonObject param = new JsonObject();
//        param.addProperty("token", HttpHelper.token);
//        param.addProperty("id", infoDetailData.getLinkId());
//        param.addProperty("type", infoDetailData.getLinkType());
//        HttpHelper.getInstance(context).headlinesTopicDetails(param, GET_TOPIC_DATA, this);
//        return true;
        return true;
    }

    /**
     * 获取话题列表数据
     * @param mode  0 代表下拉刷新，1 代表上拉加载
     * @return 代表请求提交是否成功
     */
    public boolean getTopicList(int mode){
        if(infoDetailData.getLinkId() == null || infoDetailData.getLinkId().trim().length() == 0)
            return false;
//        String topicId = infoDetailData.getTopicInfo().getTopicId();
//        if(topicId == null || topicId.trim().length() == 0)
//            return false;
        JsonObject param = new JsonObject();
        param.addProperty("token", HttpHelper.token);
//        param.addProperty("topicId", topicId);
        param.addProperty("infoId", infoDetailData.getLinkId());
        param.addProperty("type", infoDetailData.getLinkType());
        param.addProperty("mode", mode);
        List<TopicListInfo> topicList = infoDetailData.getTopicList();
        if(mode == 0) {
            if(topicList == null || topicList.size() == 0)
                param.addProperty("time", 0);
            else
                param.addProperty("time", topicList.get(0).getTime());
            HttpHelper.getInstance(context).headlinesTopicList(param, GET_TOPIC_LIST_REFRESH, this);
        } else {
            if(topicList == null || topicList.size() == 0)
                param.addProperty("time", 0);
            else
                param.addProperty("time", topicList.get(topicList.size()-1).getTime());
            HttpHelper.getInstance(context).headlinesTopicList(param, GET_TOPIC_LIST_LOAD, this);
        }
        return true;
    }

    /**
     * 资讯详情状态
     * @return 代表请求提交是否成功
     */
    public boolean getInfoDetailState(){
        if(infoDetailData.getLinkId() == null || infoDetailData.getLinkId().trim().length() == 0)
            return false;
        JsonObject param = new JsonObject();
        param.addProperty("token", HttpHelper.token);
        param.addProperty("id", infoDetailData.getLinkId());
        param.addProperty("type", infoDetailData.getLinkType());
        HttpHelper.getInstance(context).headlinesInfoDetailState(param, GET_DETAIL_STATE, this);
        return true;
    }

    @Override
    public void onStart(int requestType) {}

    @Override
    public void onCompleted(int requestType) {
        if(this.callback != null){
            this.callback.onFinish(requestType, true);
        }
    }

    @Override
    public void onError(int requestType) {
        if(this.callback != null){
            this.callback.onFinish(requestType, false);
        }
    }

    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            onError(requestType);
            return;
        }
        switch (requestType){
            case GET_DETAIL_DATA:               // 获取头条详情
                infoDetailData.setInfoData(data.getEntity(InfoData.class));
//                infoDetailData.setLinkIcon(infoDetailData.getInfoData().getInfoPic());
                break;
            case GET_TOPIC_DATA:                // 获取话题数据
                infoDetailData.setTopicInfo(data.getEntity(TopicInfo.class));
                break;
            case GET_TOPIC_LIST_REFRESH:        // 刷新获取话题列表
                infoDetailData.setTopicList(data.getList(TopicListInfo.class));
                break;
            case GET_TOPIC_LIST_LOAD:           // 加载获取话题列表
                List<TopicListInfo> getList = data.getList(TopicListInfo.class);
                List<TopicListInfo> topicList = infoDetailData.getTopicList();
                if(getList != null && getList.size() > 0){
                    if(topicList == null || topicList.size() == 0){
                        infoDetailData.setTopicList(getList);
                    } else{
                        topicList.addAll(getList);
                        infoDetailData.setTopicList(topicList);
                    }
                }
                break;
            case GET_DETAIL_STATE:              // 资讯详情状态
                InfoDetailStateModel infoDetailStateModel = data.getEntity(InfoDetailStateModel.class);
                infoDetailData.setInfoDetailStateModel(infoDetailStateModel);
                break;
        }
        onCompleted(requestType);
    }

    private GetDataCallback callback;
    public void setGetDataCallback(GetDataCallback callback){
        this.callback = callback;
    }
    public interface GetDataCallback{
        void onFinish(int requestCode, boolean isSuccess);
    }
}
